#coding=utf-8
from uliweb.orm import *

    
class MenuRole(Model):
    """
    菜单角色 (将menu表当成permission表)
    """
    name = Field(str, max_length=80, required=True)
    description = Field(str, max_length=255)
    reserve = Field(bool)
    users = ManyToMany('user', collection_name='user_menuroles')
    menus = ManyToMany('menu', through='menurole_menu_rel', collection_name='menu_menuroles')
    usergroups = ManyToMany('usergroup', collection_name='usergroup_menuroles')
    relative_usergroup = Reference('usergroup', nullable=True)
    
    def __unicode__(self):
        return self.name
    
    def usergroups_has_user(self,user):
        for usergroup in list(self.usergroups.all()):
            if usergroup.users.has(user):
                return usergroup
        return False
    
    
class MenuRole_Menu_Rel(Model):
    role = Reference('menurole')
    menu = Reference('menu')
    props = Field(PICKLE)